package com.xhui.vimarket.zkplus.zul;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.util.numeric.NumericUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.util.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpluscom.xhui.vimarket.zkplusxt;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.AttrInst;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrTmpl;
import com.xhui.vicom.xhui.vimarket.zkplusul.attr.mod.impl.AttrBaseComp;
import com.xhui.vimarket.zkplus.zulcom.xhui.vimarket.zkplusUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompUtil.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-14
 * @功能说明：属性控件
 * @修改记录： =============================================================<br>
 *        日期:2010-12-14 创建
 *        =============================================================<br>
 */
public final class AttrCompUtil {
    
    /**
     * 控件递归寻找最深层数.
     */
    private static final int    MAX_LENGTH    = 50000;
    /**
     * ATTREXT_CATTR.
     */
    private static final String ATTREXT_CATTR = "cp_attrwin";
    
    /**
     * 构造函数.
     */
    private AttrCompUtil() {
    }
    
    /**
     * 获取AttrExt component.
     * @param comp Component
     * @return AttrExt
     */
    public static AttrExt getAttrExtComp(final Component comp) {
        if (comp == null) {
            return null;
        }
        Component pcomp = comp;
        for (int i = 0; i < AttrCompUtil.MAX_LENGTH; i++) {
            if (pcomp != null && pcomp.getAttribute(AttrCompUtil.ATTREXT_CATTR) != null) {
                return (AttrExt) pcomp;
            }
            pcomp = pcomp.getParent();
        }
        return null;
    }
    
    /**
     * 获取AttrExt component.
     * @param comp Component
     * @return long
     */
    public static long getAttrCompAttrId(final Component comp) {
        return NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
    }
    
    /**
     * 获取AttrExt component.
     * @param comp Component
     * @return long
     */
    public static long getAttrCompAttrInstId(final Component comp) {
        if (comp.hasAttribute(AttrBaseComp.COMP_ATTR_INSTID)) {
            return NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTR_INSTID));
        }
        return 0;
    }
    
    /**
     * 获取AttrExt component.
     * @param comp Component
     * @return long
     */
    public static long getAttrCompIdx(final Component comp) {
        if (comp.hasAttribute(AttrBaseComp.COMP_IDX)) {
            return NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_IDX));
        }
        return 0;
    }
    
    /**
     * 设置javacode值.
     * @param attrInst AttrInst
     * @param comp Component
     */
    public static void copyTmplInfo(final AttrInst attrInst, final Component comp) {
        // 实例不为空，且界面配置了javacode，进行复制
        if (attrInst != null && comp != null
            && !"".equals(AttrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "", ""))) {
            attrInst.setJavaCode(comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "");
        }
    }
    
    /**
     * 设置javacode值.
     * @param comp Component
     * @return Object
     */
    public static Object getAttrInstData(final Component comp) {
        // 实例不为空，且界面配置了javacode，进行复制
        if (comp != null) {
            return comp.getAttribute(AttrBaseComp.COMP_DATA);
        }
        return null;
    }
    
    /**
     * 设置javacode值.
     * @param data Object
     * @param comp Component
     */
    public static void bindData(final Object data, final Component comp) {
        // 实例不为空，且界面配置了javacode，进行复制
        if (data != null && comp != null) {
            comp.setAttribute(AttrBaseComp.COMP_DATA, data);
        }
    }
    
    /**
     * 清空javacode值.
     * @param comp Component
     */
    public static void clearData(final Component comp) {
        // 实例不为空，且界面配置了javacode，进行复制
        if (comp != null) {
            comp.removeAttribute(AttrBaseComp.COMP_DATA);
            comp.removeAttribute(AttrBaseComp.COMP_ATTR_INSTID);
        }
    }
    
    /**
     * 根据界面控件根节点获取模板定义数据.
     * 
     * @param attrTmpl AttrTmpl
     * @param comp Component
     * @return AttrTmpl
     * @author wuyx
     * 2011-1-23 wuyx
     * 递归查询输入的AttrTmpl，根据attrId比较，如果相同的则返回
     * 否则返回后null，表明没有匹配
     */
    public static AttrTmpl getEqualAttrTmpl(final AttrTmpl attrTmpl, final Component comp) {
        if (attrTmpl == null || comp == null) {
            return null;
        }
        if (attrTmpl.getIdx() == NumericUtil.toLong(comp.getAttribute(AttrBaseComp.COMP_IDX) + "")
            && attrTmpl.getDisplayMode() != null
            && attrTmpl.getDisplayMode().equals(comp.getAttribute(AttrBaseComp.COMP_MODE) + "")) { // 相同则返回
            return attrTmpl;
        } else if (attrTmpl.getAttrTmplList() != null) {
            for (final AttrTmpl subAttrTmpl : attrTmpl.getAttrTmplList()) {
                final AttrTmpl subRet = AttrCompUtil.getEqualAttrTmpl(subAttrTmpl, comp);
                if (subRet != null) {
                    return subRet;
                }
            }
        }
        return null;
    }
    
    /**
     * .
     * 
     * @param cantNull boolean
     * @return String
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    public static String getStrCantNull(boolean cantNull) {
        return cantNull ? Constants.RET_TRUE
            : Constants.RET_FALSE;
    }
    
    /**
     * getCompName.
     * 
     * @param rootComp Component
     * @return String
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    public static String getCompName(Component rootComp) {
        if (rootComp == null || !rootComp.hasAttribute(AttrBaseComp.COMP_COMP_NAME)) {
            return "";
        }
        return rootComp.getAttribute(AttrBaseComp.COMP_COMP_NAME) + "";
    }
    
    /**
     * isCantNull.
     * 
     * @param rootComp Component
     * @return boolean
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    public static boolean isCantNull(Component rootComp) {
        if (rootComp == null || !rootComp.hasAttribute(AttrBaseComp.COMP_CANTNULL)) {
            return false;
        }
        return Constants.RET_TRUE.equals(rootComp.getAttribute(AttrBaseComp.COMP_CANTNULL) + "") ? true
            : false;
    }
    
    /**
     * .
     * 
     * @param type String
     * @param retMsg String
     * @param rootComp Component
     * @return List<RetVO>
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    public static List<RetVO> getRetVoList(String type, String retMsg, Component rootComp) {
        RetVO retVo = new RetVO();
        retVo.setResult(type);
        retVo.setRetMsg(retMsg);
        retVo.setObject(rootComp);
        List<RetVO> list = new ArrayList<RetVO>();
        list.add(retVo);
        return list;
    }
    
    /**
     * setCompAttr.
     * 
     * @param comp Component
     * @param tag String
     * @param obj Object
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static void setCompAttr(Component comp, String tag, Object obj) {
        if (comp != null && obj != null) {
            comp.setAttribute(tag, obj);
        }
    }
    
    /**
     * getCompAttrStr.
     * 
     * @param comp Component
     * @param tag String
     * @return String
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static String getCompAttrStr(Component comp, String tag) {
        if (comp != null && comp.hasAttribute(tag)) {
            return comp.getAttribute(tag) == null ? null
                : "" + comp.getAttribute(tag);
        }
        return null;
    }
    
    /**
     * 获取AttrExt component.
     * @param comp Component
     * @return String
     */
    public static String getAttrCompMode(final Component comp) {
        return "" + comp.getAttribute(AttrBaseComp.COMP_MODE);
    }
    
    /**
     * resetMustRule.
     * 
     * @param root Component
     * @param ruleFlag boolean
     * @author wuyx
     * 2011-1-25 wuyx 
     */
    public static void resetMustRule(Component root, boolean ruleFlag) {
        // 取按钮
        // hbox // div(0) // Span(4)
        if (root == null) {
            return;
        }
        Span span = null;
        if(root.getFirstChild().getChildren().get(4) instanceof Span){
            span =(Span) root.getFirstChild().getChildren().get(4);
        } else if(root.getFirstChild().getChildren().get(3) instanceof Span){
            span =(Span) root.getFirstChild().getChildren().get(3);
        }
        if(span!=null) {
            if (span.getFirstChild() == null) {
                if (ruleFlag) {
                    Label label = new Label();
                    label.setValue(" *");
                    label.setStyle("color:red;");
                    label.setParent(span);
                } else {
                    //
                }
            } else {
                Label label = (Label) span.getFirstChild();
                if (ruleFlag) {
                    label.setValue(" *");
                    label.setStyle("color:red;");
                } else {
                    label.setValue("");
                }
            }
        }
    }
    
    /**
     * 根据上级控件类型获取首个上级的控件.
     * @param thisComp 当前控件
     * @param fMode 查找的控件类型
     * @return Component
     */
    public static Component getSupCompRootByMode(final Component thisComp, final String fMode) {
        if (thisComp == null) {
            return null;
        }
        // 
        Component theComp = thisComp;
        for (int i = 0; i < AttrCompUtil.MAX_LENGTH; i++) {
            if (theComp == null || theComp instanceof AttrExt) {
                return null;
            }
            if (theComp.hasAttribute(AttrBaseComp.COMP_MODE)) {
                String theIMode = AttrCompUtil.getAttrCompMode(theComp);
                if (theIMode.equals(fMode)) {
                    return theComp;
                }
            }
            theComp = theComp.getParent();
        }
        return null;
    }
}
